using System;
using System.Collections.Generic;
using System.Globalization;
using BitMango.Diagnostics;
using HCP;
using UnityEngine;

namespace BitMango
{
    public static class Profile
    {
        static Profile()
        {
            int @int = PlayerPrefs.GetInt("Profile.userLv", Profile.userLv);
            Profile.SetUserLv(@int);
        }

        public static void Init()
        {
            Profile.CheckCurrentVersion();
        }

        public static int UserLv
        {
            get
            {
                return Profile.userLv;
            }
        }

        private static float GetFloat4Bit(int iv)
        {
            return (float)iv / 4f;
        }

        public static bool IsSuperUser
        {
            get
            {
                return (Profile.UserLv & 1073741824) != 0;
            }
        }

        public static bool IsLogLocal
        {
            get
            {
                return (Profile.UserLv & 536870912) != 0;
            }
        }

        public static bool IsLogRemote
        {
            get
            {
                return (Profile.UserLv & 268435456) != 0;
            }
        }

        public static bool IsEnableCheat
        {
            get
            {
                return (Profile.UserLv & 134217728) != 0;
            }
        }

        public static bool IsAdTest
        {
            get
            {
                return (Profile.UserLv & 67108864) != 0;
            }
        }

        public static bool IsDummyIAP
        {
            get
            {
                return (Profile.UserLv & 33554432) != 0;
            }
        }

        public static bool IsDummyAD
        {
            get
            {
                return (Profile.UserLv & 16777216) != 0;
            }
        }

        public static bool IsEchoInput
        {
            get
            {
                return (Profile.UserLv & 8388608) != 0;
            }
        }

        public static bool IsBigMouth
        {
            get
            {
                return (Profile.UserLv & 4194304) != 0;
            }
        }

        public static bool IsIapWhale
        {
            get
            {
                return (Profile.UserLv & 16384) != 0;
            }
        }

        public static bool IsIapCracker
        {
            get
            {
                return (Profile.UserLv & 8192) != 0;
            }
        }

        public static float AdFrequency
        {
            get
            {
                return Profile.GetFloat4Bit((Profile.UserLv & 3840) >> 8);
            }
        }

        public static string UUID
        {
            get
            {
                if (Profile._UUID == null)
                {
                    if (PlayerPrefs.HasKey("UUID"))
                    {
                        Profile._UUID = PlayerPrefs.GetString("UUID");
                    }
                    else
                    {
                        Profile._UUID = Guid.NewGuid().ToString() + "_GUID";
                        PlayerPrefs.SetString("UUID", Profile._UUID);
                        PlayerPrefs.Save();
                    }
                }
                return Profile._UUID;
            }
        }

        public static string ADID
        {
            get
            {
                if (Profile._ADID == null)
                {
                    if (string.IsNullOrEmpty(Profile._ADID))
                    {
                        Debugger.LogWarning("Profile.ADID not found using UUID");
                        Profile._ADID = Profile.UUID;
                    }
                }
                return Profile._ADID;
            }
        }

        public static string InstallVersion
        {
            get
            {
                if (Profile.installVersion == null)
                {
                    if (PlayerPrefs.HasKey("InstallVersion"))
                    {
                        Profile.installVersion = PlayerPrefs.GetString("InstallVersion");
                    }
                    else
                    {
                    }
                }
                return Profile.installVersion;
            }
            set
            {
                Profile.installVersion = value;
                PlayerPrefs.SetString("InstallVersion", Profile.installVersion);
                PlayerPrefs.Save();
            }
        }

        public static bool IsVersionChanged
        {
            get
            {
                return Profile.isVersionChanged;
            }
        }

        public static string OldVersion
        {
            get
            {
                return Profile.oldVersion;
            }
        }

        public static string CurrentVersion
        {
            get
            {
                return Profile.currentVersion;
            }
        }

        public static void CheckCurrentVersion()
        {
            Profile.oldVersion = PlayerPrefs.GetString("CurrentVersion", Profile.InstallVersion);
            if (Profile.oldVersion != Profile.currentVersion)
            {
                UnityEngine.Debug.Log("Profile.OnAppVersionChanged " + Profile.oldVersion + " => " + Profile.currentVersion);
                Platform.BroadcastMessage("OnAppVersionChanged", new string[]
                {
                    Profile.oldVersion,
                    Profile.currentVersion
                });
                Profile.isVersionChanged = true;
            }
            PlayerPrefs.SetString("CurrentVersion", Profile.currentVersion);
        }

        public static DateTime InstallTime
        {
            get
            {
                if (Profile.installTime == DateTime.MinValue)
                {
                    string text = PlayerPrefs.GetString("InstallTime", string.Empty);
                    if (text == string.Empty)
                    {
                        Profile.InstallTime = DateTime.Now;
                    }
                    else
                    {
                        text = WWW.UnEscapeURL(text);
                        if (!DateTime.TryParseExact(text, "yyyy-MM-dd HH:mm:ss GMT", CultureInfo.InvariantCulture, DateTimeStyles.None, out Profile.installTime))
                        {
                            UnityEngine.Debug.LogError("Profile.InstallTime parse Error : " + text);
                            Profile.InstallTime = DateTime.Now;
                        }
                    }
                }
                return Profile.installTime;
            }
            set
            {
                Profile.installTime = value;
                Debugger.Log("Profile.InstallTime is set to " + Profile.installTime.ToString());
                string installTimeStr = Profile.InstallTimeStr;
                PlayerPrefs.SetString("InstallTime", installTimeStr);
                PlayerPrefs.Save();
            }
        }

        public static string InstallTimeStr
        {
            get
            {
                return WWW.EscapeURL(Profile.InstallTime.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss") + " GMT");
            }
        }

        public static string LastStageName
        {
            get
            {
                return PlayerPrefs.GetString("DWATT.LastStage", "initial");
            }
            set
            {
                PlayerPrefs.SetString("DWATT.LastStage", value);
                PlayerPrefs.SetString("FirebaseAnalytics.LastStage", value);
                PlayerPrefs.Save();
            }
        }

        private static float ElapseTime
        {
            get
            {
                return DWATTHelper.ElapseTime;
            }
        }

        public static float DailySessionTime
        {
            get
            {
                float num;
                if (PlayerPrefs.HasKey("DailySessionDate") && PlayerPrefs.GetString("DailySessionDate") == DateTime.UtcNow.ToString("yyyy-MM-dd"))
                {
                    num = ((!PlayerPrefs.HasKey("DailySessionTime")) ? Profile.ElapseTime : (PlayerPrefs.GetFloat("DailySessionTime") + Profile.ElapseTime));
                    PlayerPrefs.SetFloat("DailySessionTime", num);
                    PlayerPrefs.Save();
                }
                else
                {
                    PlayerPrefs.SetString("DailySessionDate", DateTime.UtcNow.ToString("yyyy-MM-dd"));
                    PlayerPrefs.SetFloat("DailySessionTime", Profile.ElapseTime);
                    PlayerPrefs.Save();
                    num = Profile.ElapseTime;
                }
                return num;
            }
        }

        public static int DailySessionCount
        {
            get
            {
                int result = 0;
                if (PlayerPrefs.HasKey("DailySessionCountDate") && PlayerPrefs.GetString("DailySessionCountDate") == DateTime.UtcNow.ToString("yyyy-MM-dd"))
                {
                    result = ((!PlayerPrefs.HasKey("DailySessionCount")) ? 0 : PlayerPrefs.GetInt("DailySessionCount"));
                }
                else
                {
                    PlayerPrefs.SetString("DailySessionCountDate", DateTime.UtcNow.ToString("yyyy-MM-dd"));
                    PlayerPrefs.SetInt("DailySessionCount", 0);
                    PlayerPrefs.Save();
                }
                return result;
            }
            set
            {
                PlayerPrefs.SetInt("DailySessionCount", value);
                PlayerPrefs.Save();
            }
        }

        public static float AccumSessionTime
        {
            get
            {
                if (PlayerPrefs.HasKey("AccumSessionTime"))
                {
                    return PlayerPrefs.GetFloat("AccumSessionTime") + Profile.ElapseTime;
                }
                return Profile.ElapseTime;
            }
            set
            {
                PlayerPrefs.SetFloat("AccumSessionTime", value);
                PlayerPrefs.Save();
            }
        }

        public static int AccumSessionCount
        {
            get
            {
                if (PlayerPrefs.HasKey("AccumSessionCount"))
                {
                    return PlayerPrefs.GetInt("AccumSessionCount");
                }
                return 0;
            }
            set
            {
                PlayerPrefs.SetInt("AccumSessionCount", value);
                PlayerPrefs.Save();
            }
        }

        public static bool IsServerReportDate
        {
            get
            {
                if (PlayerPrefs.HasKey("UserStaticsReportTime"))
                {
                    try
                    {
                        DateTime t = DateTime.Parse(PlayerPrefs.GetString("UserStaticsReportTime"));
                        return DateTime.Compare(DateTime.Now, t) > 0;
                    }
                    catch (Exception ex)
                    {
                        Debugger.LogError("Profile.IsServerReportDate DateTime Parse error" + PlayerPrefs.GetString("UserStaticsReportTime") + ", error message : " + ex.ToString());
                    }
                    return true;
                }
                return true;
            }
        }

        public static short DaysAfterInstall
        {
            get
            {
                DateTime d = Convert.ToDateTime(Profile.InstallTime.ToUniversalTime().ToString("yyyy-MM-dd"));
                DateTime utcNow = DateTime.UtcNow;
                return Convert.ToInt16((utcNow - d).Days);
            }
        }

        public static string IntegrityCheckKey
        {
            get
            {
                return PlayerPrefs.GetString("UserStatics.ICK", string.Empty);
            }
            set
            {
                PlayerPrefs.SetString("UserStatics.ICK", value);
                PlayerPrefs.Save();
            }
        }

        public static void SetUserLv(int lv)
        {
            if (lv == 0 || lv < 0)
            {
                Debugger.LogError("Profile.SetUserLv out of range set to default : " + lv);
                lv = 1024;
            }
            Profile.userLv = lv;
            PlayerPrefs.SetInt("Profile.userLv", Profile.userLv);
            Debugger.EnableLocalLog = Profile.IsLogLocal;
            Debugger.Log(string.Concat(new object[]
            {
                "Profile.SetUserLv: ",
                lv,
                "  ",
                Profile.ToString()
            }));
            if (Application.isPlaying && Profile.IsAdTest && GameObject.Find("AppiumServer") == null)
            {
                GameObject gameObject = new GameObject();
                gameObject.name = "AppiumServer";
                gameObject.AddComponent<Server>();
                UnityEngine.Object.DontDestroyOnLoad(gameObject);
            }
        }

        public static string ToString()
        {
            return string.Concat(new object[]
            {
                "SU(",
                Profile.IsSuperUser,
                ") LL(",
                Profile.IsLogLocal,
                ") LR(",
                Profile.IsLogRemote,
                ") EC(",
                Profile.IsEnableCheat,
                ") AT(",
                Profile.IsAdTest,
                ") DI(",
                Profile.IsDummyIAP,
                ") DA(",
                Profile.IsDummyAD,
                ") EI(",
                Profile.IsEchoInput,
                ") IW(",
                Profile.IsIapWhale,
                ") IC(",
                Profile.IsIapCracker,
                ") FQ(",
                Profile.AdFrequency,
                ")"
            });
        }


        public static float FPS
        {
            get
            {
                float num = 0f;
                foreach (float num2 in Profile.fpsHistory)
                {
                    float num3 = num2;
                    num += num3;
                }
                return num / (float)Profile.fpsHistory.Count;
            }
        }

        public static void UpdateFPSHistory(float fps)
        {
            Profile.fpsHistory.Enqueue(fps);
            if (Profile.fpsHistory.Count > 120)
            {
                Profile.fpsHistory.Dequeue();
            }
        }

        public const int SUPERUSER = 1073741824;

        public const int LOG_LOCAL = 536870912;

        public const int LOG_REMOTE = 268435456;

        public const int ENABLE_CHEAT = 134217728;

        public const int AD_TEST = 67108864;

        public const int DUMMY_IAP = 33554432;

        public const int DUMMY_AD = 16777216;

        public const int ECHO_INPUT = 8388608;

        public const int BIG_MOUTH = 4194304;

        public const int IAP_WHALE = 16384;

        public const int IAP_CRACKER = 8192;

        public const int AD_FREQ = 3840;

        public const int F_ONE = 4;

        public const int F_TWO = 8;

        public const int F_THREE = 12;

        public const int F_HALF = 2;

        public const int F_QUARTER = 1;

        public const int DEFAULT_USER_LV = 1024;

        private static int userLv = 1024;

        private static string _UUID;

        private static string _ADID;

        private static DateTime installTime = DateTime.MinValue;

        private static string installVersion;

        private static bool isVersionChanged = false;

        private static string oldVersion;

        private static string currentVersion;

        private static float usedMaxMemory = 0f;

        private static Queue<float> fpsHistory = new Queue<float>();
    }
}
